package com.gzhryc.shared_device.oem.code.member.services;

import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.shared_device.oem.code.member.dao.MemberAddressDao;
import com.gzhryc.shared_device.oem.code.member.dao.db.MemberAddress;
import com.gzhryc.shared_device.oem.code.member.dao.enums.MemberAddressEnum;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class MemberAddressService {

	static Logger log = Logger.getLogger(MemberAddressService.class);

	public static MemberAddressService self() {
		return MultiDBTools.getService(MemberAddressService.class, true);
	}

	public static MemberAddressService self(String jdbcKey) {
		return MultiDBTools.getService(jdbcKey, MemberAddressService.class, true);
	}

	private String jdbcKey;

	public MemberAddressService(String jdbcKey) {
		this.jdbcKey = jdbcKey;
	}

	public List<MemberAddress> findByMemberId(Long memberId,Integer pageNo,Integer pageLimit){
		if(memberId != null){
			MemberAddressDao dao = new MemberAddressDao(jdbcKey,memberId);

			Conditions conditions = new Conditions(MemberAddress.class);
			conditions.and().is("memberId",memberId);

			try {
				return dao.findByCondition(conditions,pageNo,pageLimit);
			} catch (SQLException e) {
				log.error(e.getMessage(),e);
			}
		}
		return null;
	}

	public Long countByMemberId(Long memberId){
		if(memberId != null){
			MemberAddressDao dao = new MemberAddressDao(jdbcKey,memberId);

			Conditions conditions = new Conditions(MemberAddress.class);
			conditions.and().is("memberId",memberId);

			try {
				return dao.count(conditions);
			} catch (SQLException e) {
				log.error(e.getMessage(),e);
			}
		}
		return 0L;
	}

	public MemberAddress getByKeys(Long memberId, Long addressId) {
		if (memberId != null && addressId != null) {
			MemberAddressDao dao = new MemberAddressDao(jdbcKey,memberId);

			Conditions conditions = new Conditions(MemberAddress.class);
			conditions.and().is("id",addressId);
			conditions.and().is("memberId",memberId);

			try {
				return dao.get(conditions);
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			}
		}
		return null;
	}

	public MemberAddress getDefByMemberId(Long memberId){
		if(memberId != null){
			MemberAddressDao dao = new MemberAddressDao(jdbcKey,memberId);

			Conditions conditions = new Conditions(MemberAddress.class);
			conditions.and().is("memberId",memberId);
			conditions.and().is("state", MemberAddressEnum.State.Default.index());

			try {
				return dao.get(conditions);
			} catch (SQLException e) {
				log.error(e.getMessage(),e);
			}
		}
		return null;
	}

	public boolean add(MemberAddress memberAddress)throws LogicException {
		memberAddress.setCreateDate(new Date());
		MemberAddress temp = getDefByMemberId(memberAddress.getMemberId());
		if(temp != null){
			memberAddress.setState(MemberAddressEnum.State.Normal.index());
		}else{
			memberAddress.setState(MemberAddressEnum.State.Default.index());
		}

		MemberAddressDao dao = new MemberAddressDao(jdbcKey,memberAddress.getMemberId());

		try {
			return dao.insert(memberAddress) > 0;
		} catch (SQLException e) {
			log.error(e.getMessage(),e);
		}
		return false;
	}

	public boolean updateNotNull(MemberAddress memberAddress)throws LogicException{
		MemberAddressDao dao = new MemberAddressDao(jdbcKey,memberAddress.getMemberId());
		try {
			return dao.updateNotNull(memberAddress) > 0;
		} catch (SQLException e) {
			log.error(e.getMessage(),e);
		}
		return false;
	}

	public boolean deleteByKeys(Long memberId,Long addressId) throws LogicException{
		if (memberId != null && addressId != null) {
			MemberAddressDao dao = new MemberAddressDao(jdbcKey,memberId);

			Conditions conditions = new Conditions(MemberAddress.class);
			conditions.and().is("id",addressId);
			conditions.and().is("memberId",memberId);

			try {
				return dao.deleteByCondition(conditions) > 0;
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			}
		}
		return false;
	}
}
